<?php

namespace App\Http\Controllers;

use App\Api\Models\Member;
use App\Api\Services\JsonService;
use App\Api\Services\Token;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

/**
 * 用户登录类
 * Class AuthController
 * @package App\Http\Controllers
 */
class AuthController extends Controller
{
    //
    public function login(Request $request){
        $user = Member::where('phone', $request->post('phone'))->find();
        if ($user) {
            if ($user->pwd !== encrypt($request->post('password')))
                return app('json')->fail('账号或密码错误');
            if ($user->pwd === encrypt(123456))
                return JsonService::fail('请修改您的初始密码，再尝试登陆！');
        } else {
            return JsonService::fail('账号或密码错误');
        }
        if (!$user['status'])
            return JsonService::fail('已被禁止，请联系管理员');




        $token = UserToken::createToken($user, 'user');
        $token = Token::MakeToken(['member_id' => $user->member_id]);

        if ($token) {
            event('UserLogin', [$user, $token]);
            return JsonService::success('登录成功', ['token' => $token->token, 'expires_time' => $token->expires_time]);
        } else
            return JsonService::fail('登录失败');
    }
}
